/******************************************************************/
/*                                                                */
/*  Winpooch : Windows Watchdog                                   */
/*  Copyright (C) 2004-2005  Benoit Blanchon                      */
/*                                                                */
/*  This program is free software; you can redistribute it        */
/*  and/or modify it under the terms of the GNU General Public    */
/*  License as published by the Free Software Foundation; either  */
/*  version 2 of the License, or (at your option) any later       */
/*  version.                                                      */
/*                                                                */
/*  This program is distributed in the hope that it will be       */
/*  useful, but WITHOUT ANY WARRANTY; without even the implied    */
/*  warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR       */
/*  PURPOSE.  See the GNU General Public License for more         */
/*  details.                                                      */
/*                                                                */
/*  You should have received a copy of the GNU General Public     */
/*  License along with this program; if not, write to the Free    */
/*  Software Foundation, Inc.,                                    */
/*  675 Mass Ave, Cambridge, MA 02139, USA.                       */
/*                                                                */
/******************************************************************/


/******************************************************************/
/* Includes                                                       */
/******************************************************************/

// module's interface
#include "ConfigWnd.h"

// standard headers
#include <windows.h>
#include <windowsx.h>
#include <commctrl.h>
#include <tchar.h>

// project's headers
#include "Assert.h"
#include "Config.h"
#include "Language.h"
#include "LogFile.h"
#include "ProjectInfo.h"
#include "Resources.h"
#include "Scanner.h"
#include "SpyServer.h"
#include "Trace.h"


/******************************************************************/
/* Internal macros                                                */
/******************************************************************/

#define arraysize(a) (sizeof(a)/sizeof((a)[0]))
#define clear(a) memset(&(a),0,sizeof(a))


/******************************************************************/
/* Internal data types                                            */
/******************************************************************/

typedef struct {
  UINT		idCtrl ;
  UINT		idCfgInt ;
  UINT		idStr ;
} CONFIG_CHECKBOX ;


/******************************************************************/
/* Internal constants                                             */
/******************************************************************/

#define WM_UPDATECONFIG	(WM_USER+10)

static CONFIG_CHECKBOX aCheckBoxes[] = {
  { IDC_DEBUG_PRIVILEGE,	CFGINT_DEBUG_PRIVILEGE },
  { IDC_SPLASH_SCREEN,		CFGINT_SPLASH_SCREEN },
  { IDC_CHECK_UPDATES,		CFGINT_CHECK_FOR_UPDATES },
  { IDC_SOUND,			CFGINT_SOUND } 
} ;



/******************************************************************/
/* Internal functions                                             */
/******************************************************************/

BOOL CALLBACK _ConfigWnd_DlgProc (HWND, UINT, WPARAM, LPARAM) ;


/******************************************************************/
/* Exported function : RegisterClass                              */
/******************************************************************/

BOOL ConfigWnd_RegisterClass (HINSTANCE hInstance) 
{

  return TRUE ;
}


/******************************************************************/
/* Exported function : CreateWindow                               */
/******************************************************************/

HWND ConfigWnd_CreateWindow (HINSTANCE hInstance, HWND hwndParent)
{  
  return CreateDialog (hInstance, MAKEINTRESOURCE(DLG_CONFIG),
		       hwndParent, _ConfigWnd_DlgProc) ;
}


/******************************************************************/
/* Internal function : WndProc                                    */
/******************************************************************/

BOOL CALLBACK _ConfigWnd_DlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
  int		i ;
  UINT		nSize ;
  BOOL		bChecked ;
  TCHAR		szBuffer[1024] ;
/*
  RECT	rect ;
  HDC	hdc ;
 */

  switch( message )
    {
    case WM_INITDIALOG:

      // Not available in this version
      EnableWindow (GetDlgItem(hDlg,IDC_CHECK_UPDATES), FALSE) ;
      EnableWindow (GetDlgItem(hDlg,IDC_SOUND), FALSE) ;

      // set language list
      for( i=0 ; i<Language_GetLanguageCount() ; i++ )
	SendDlgItemMessage (hDlg, IDC_LANGUAGE, CB_ADDSTRING, 0,
			    (LPARAM)Language_GetLanguage(i)) ;     

    case WM_LANGUAGECHANGED:

      // add antivirus
      SendDlgItemMessage (hDlg, IDC_ANTIVIRUS, CB_RESETCONTENT, 0, 0) ; 
      SendDlgItemMessage (hDlg, IDC_ANTIVIRUS, CB_ADDSTRING, 0,
			  (LPARAM) STR_DEF(_ANTIVIRUS_NONE,TEXT("None"))) ;
      SendDlgItemMessage (hDlg, IDC_ANTIVIRUS, CB_ADDSTRING, 0,
			  (LPARAM) STR_DEF(_ANTIVIRUS_CLAMWIN,TEXT("ClamWin"))) ;

      // set language dependant strings
      if( Language_IsLoaded() )
	{
	  SendDlgItemMessage (hDlg, IDC_LANGUAGE, CB_SELECTSTRING, 0,
			      (LPARAM)STR(_LANGUAGE_NAME_IN_ENGLISH)) ;      
	  SetDlgItemText (hDlg, IDT_ANTIVIRUS, STR(_ANTIVIRUS)) ;
	  SetDlgItemText (hDlg, IDT_LANGUAGE, STR(_LANGUAGE)) ;
	  SetDlgItemText (hDlg, IDC_DEBUG_PRIVILEGE, STR(_USE_DEBUG_PRIVILEGE)) ;
	  SetDlgItemText (hDlg, IDC_SPLASH_SCREEN, STR(_ENABLE_SPLASH_SCREEN)) ;
	  SetDlgItemText (hDlg, IDC_CHECK_UPDATES, STR(_CHECK_FOR_UPDATES)) ;
	  SetDlgItemText (hDlg, IDC_SOUND, STR(_ENABLE_SOUND)) ;
	  SetDlgItemText (hDlg, IDT_MAX_LOG_SIZE, STR(_MAX_LOG_FILE_SIZE)) ;
	  SetDlgItemText (hDlg, IDT_KILOBYTES, STR(_KILOBYTES)) ;
	  SetDlgItemText (hDlg, IDC_APPLY, STR(_APPLY_CONFIGURATION)) ;
	}

    case WM_UPDATECONFIG:

      // antivirus
      SendDlgItemMessage (hDlg, IDC_ANTIVIRUS, CB_SETCURSEL, 
			  Scanner_GetScanner(), 0) ;

      // check boxes
      for( i=0 ; i<arraysize(aCheckBoxes) ; i++ )
	CheckDlgButton (hDlg, aCheckBoxes[i].idCtrl, 
			Config_GetInteger(aCheckBoxes[i].idCfgInt)
			? BST_CHECKED : BST_UNCHECKED) ;      

      // max log size
      SetDlgItemInt (hDlg, IDC_MAX_LOG_SIZE,
		     Config_GetInteger(CFGINT_MAX_LOG_SIZE)>>10,
		     FALSE) ;
      
      EnableWindow (GetDlgItem(hDlg,IDC_APPLY), FALSE) ;

      return TRUE ;

    case WM_COMMAND:

     /* switch( LOWORD(wParam) )
	{
	case IDC_DEBUG_PRIVILEGE:
	case IDC_SPLASH_SCREEN: 
	case IDC_CHECK_UPDATES: 
	case IDC_SOUND: 
	  EnableWindow (GetDlgItem(hDlg,IDC_APPLY), TRUE) ;
	  return TRUE ;
	}*/
     switch( wParam )
	{
	case MAKELONG(IDC_DEBUG_PRIVILEGE,BN_CLICKED):
	case MAKELONG(IDC_SPLASH_SCREEN,BN_CLICKED): 
	case MAKELONG(IDC_CHECK_UPDATES,BN_CLICKED): 
	case MAKELONG(IDC_SOUND,BN_CLICKED): 
	case MAKELONG(IDC_MAX_LOG_SIZE,EN_CHANGE): 
	case MAKELONG(IDC_LANGUAGE,CBN_SELCHANGE): 
	case MAKELONG(IDC_ANTIVIRUS,CBN_SELCHANGE): 
	  EnableWindow (GetDlgItem(hDlg,IDC_APPLY), TRUE) ;
	  return TRUE ;

	case MAKELONG(IDC_APPLY,BN_CLICKED): 

	  // for each check box
	  for( i=0 ; i<arraysize(aCheckBoxes) ; i++ ) {
	    bChecked = IsDlgButtonChecked (hDlg, aCheckBoxes[i].idCtrl) == BST_CHECKED ;
	    Config_SetInteger (aCheckBoxes[i].idCfgInt, bChecked) ;
	  }

	  // max log size
	  nSize = 1024 * GetDlgItemInt (hDlg, IDC_MAX_LOG_SIZE, NULL, FALSE) ;	  
	  Config_SetInteger (CFGINT_MAX_LOG_SIZE, nSize) ;	 

	  // antivirus
	  if( ! Scanner_SetScanner(SendDlgItemMessage(hDlg,IDC_ANTIVIRUS,CB_GETCURSEL,0,0)) )
	    MessageBox (hDlg, STR_DEF(_ANTIVIRUS_NOT_INSTALLED,
				      TEXT("The antivirus you selected isn't installed")),
			TEXT(APPLICATION_NAME), MB_ICONERROR) ;

	  // language
	  GetDlgItemText (hDlg, IDC_LANGUAGE, szBuffer, 64) ;
	  Language_LoadByName (szBuffer) ;
	  
	  LogFile_ReloadConfig () ;
	  SpySrv_ReloadConfig () ;

	  SendMessage (hDlg, WM_UPDATECONFIG, 0, 0) ;
	}

      return FALSE ;

/*
    case WM_ERASEBKGND:
      hdc = (HDC) wParam ;

      GetClientRect (hDlg, &rect) ;
      FillRect (hdc, &rect, GetStockObject(WHITE_BRUSH)) ;
      
      SetWindowLong (hDlg, DWL_MSGRESULT, TRUE) ;
      return TRUE ;
 */
    }

  return FALSE ;
}
